{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Ways to Split Array Into Good Subarrays"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: numberOfGoodSubarraySplits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #将数组划分成若干好子数组的方式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个二元数组 <code>nums</code> 。</p>\n",
    "\n",
    "<p>如果数组中的某个子数组 <strong>恰好</strong> 只存在 <strong>一</strong> 个值为 <code>1</code> 的元素，则认为该子数组是一个 <strong>好子数组</strong> 。</p>\n",
    "\n",
    "<p>请你统计将数组 <code>nums</code> 划分成若干 <strong>好子数组</strong> 的方法数，并以整数形式返回。由于数字可能很大，返回其对 <code>10<sup>9</sup> + 7</code> <strong>取余 </strong>之后的结果。</p>\n",
    "\n",
    "<p>子数组是数组中的一个连续 <strong>非空</strong> 元素序列。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [0,1,0,0,1]\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：</strong>存在 3 种可以将 nums 划分成若干好子数组的方式：\n",
    "- [0,1] [0,0,1]\n",
    "- [0,1,0] [0,1]\n",
    "- [0,1,0,0] [1]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [0,1,0]\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>存在 1 种可以将 nums 划分成若干好子数组的方式：\n",
    "- [0,1,0]\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>0 &lt;= nums[i] &lt;= 1</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [ways-to-split-array-into-good-subarrays](https://leetcode.cn/problems/ways-to-split-array-into-good-subarrays/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [ways-to-split-array-into-good-subarrays](https://leetcode.cn/problems/ways-to-split-array-into-good-subarrays/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[0,1,0,0,1]', '[0,1,0]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        # 1中间夹的0的数量+1相乘\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 1:\n",
    "                break\n",
    "        else:\n",
    "            return 0\n",
    "        cnt = 1\n",
    "        ans = 1\n",
    "        for x in nums[i+1:]:\n",
    "            if x == 0:\n",
    "                cnt += 1\n",
    "            else:\n",
    "                ans *= cnt\n",
    "                cnt = 1\n",
    "        return ans % (10 ** 9 + 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        # 1中间夹的0的数量+1相乘\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 1:\n",
    "                break\n",
    "        else:\n",
    "            return 0\n",
    "        cnt = 1\n",
    "        ans = 1\n",
    "        for x in nums[i+1:]:\n",
    "            if x == 0:\n",
    "                cnt += 1\n",
    "            else:\n",
    "                ans *= cnt\n",
    "                cnt = 1\n",
    "        return ans % (10 ** 9 + 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        MOD = 10 ** 9 + 7\n",
    "        ans, pre = 1, -1\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 0: continue\n",
    "            if pre >= 0:\n",
    "                ans = ans * (i - pre) % MOD\n",
    "            pre = i\n",
    "        return 0 if pre < 0 else ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        if nums.count(1)==0 :\n",
    "            return 0\n",
    "        if nums.count(1)==1 :\n",
    "            return 1\n",
    "        mod=10**9+7\n",
    "        ans=1\n",
    "        i,j=0,0\n",
    "        n=len(nums)\n",
    "        while i<n :\n",
    "            while i<n and nums[i]!=1 :\n",
    "                i+=1\n",
    "            j=i+1\n",
    "            while j<n and nums[j]!=1 :\n",
    "                j+=1\n",
    "            if j==n :\n",
    "                break\n",
    "            ans=(ans*(j-i))%mod\n",
    "            i=j\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        interval = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == 1 and not interval:\n",
    "                start = i\n",
    "                interval.append(1)\n",
    "            elif nums[i] == 1 and interval:\n",
    "                interval.append(i - start)\n",
    "                start = i\n",
    "        ans = 1\n",
    "        for i in interval:\n",
    "            ans *= i\n",
    "            ans %= (1e9 + 7)\n",
    "        return int(ans) if interval else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        Mod1 = 10 ** 9 + 7\n",
    "        ans = 1\n",
    "        pre = -1\n",
    "        for i,x in enumerate(nums):\n",
    "            if x == 0:\n",
    "                continue\n",
    "            if pre >= 0:\n",
    "                ans = ans * (i - pre) % Mod1\n",
    "            pre = i\n",
    "        return 0 if pre < 0 else ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        ans=1\n",
    "        pre=-1\n",
    "        for i,x in enumerate(nums):\n",
    "            if x == 0:\n",
    "                continue\n",
    "            if pre != -1:\n",
    "                ans*= (i-pre)\n",
    "                ans%= (10**9+7)\n",
    "            pre=i \n",
    "        return ans if pre != -1 else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        # res = []\n",
    "        # i =0 \n",
    "        # for idx, num in enumerate(nums):\n",
    "        #     if num ==1:\n",
    "        #         res.append(idx -i)\n",
    "        #         i = idx\n",
    "        # if len(res) ==0 :\n",
    "        #     return 0\n",
    "        # resnum =1 \n",
    "        # for idx,num in enumerate(res):\n",
    "        #     if idx !=0:\n",
    "        #         resnum*=num\n",
    "        # return int(resnum %(1e9 + 7))\n",
    "        MOD = 10**9 + 7\n",
    "        ans, pre = 1, -1\n",
    "        for i ,x in enumerate(nums):\n",
    "            if x ==0 :\n",
    "                continue \n",
    "            if pre >=0:\n",
    "                ans = ans *(i - pre) %MOD\n",
    "            pre = i\n",
    "        return 0 if pre <0 else ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        MOD = 10 ** 9 + 7\n",
    "        ans, pre = 1, -1\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 0: continue\n",
    "            if pre >= 0:\n",
    "                ans = ans * (i - pre) % MOD\n",
    "            pre = i\n",
    "        return 0 if pre < 0 else ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        ans, prev = 1, -1\n",
    "        mod = 10**9 + 7\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 0: continue\n",
    "            if prev >= 0:\n",
    "                ans = ans * (i - prev) % mod\n",
    "            prev = i\n",
    "        return 0 if prev < 0 else ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        flag=0\n",
    "        res=1\n",
    "        count=0\n",
    "        for x in nums:\n",
    "            if x==1:                               \n",
    "                if flag==0:\n",
    "                    flag=1\n",
    "                    count=0\n",
    "                    continue\n",
    "                flag=1\n",
    "                res*=(count+1)\n",
    "                count=0\n",
    "            else:\n",
    "                count+=1\n",
    "        return (res%1000000007)*flag "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        if nums.count(1) == 0:\n",
    "            return 0\n",
    "        res = 1\n",
    "        i = nums.index(1)\n",
    "        j = i+1\n",
    "        while j < len(nums):\n",
    "            if nums[j] == 1:\n",
    "                res *= j-i\n",
    "                i = j\n",
    "            j+=1\n",
    "        return res%(10**9+7)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        #leading_zero_cnt = -1\n",
    "        #tailing_zero_cnt = -1\n",
    "        start, end = 0, n - 1\n",
    "        while start < n and nums[start] == 0:\n",
    "            start += 1\n",
    "        while end >= 0 and nums[end] == 0:\n",
    "            end -= 1\n",
    "        #leading_zero_cnt, tailing_zero_cnt = start, n - 1 - end\n",
    "        if start > end:\n",
    "            return 0\n",
    "        ans = 1\n",
    "        MOD = 10 ** 9 + 7\n",
    "        i = start + 1\n",
    "        while i < end:\n",
    "            if nums[i] == 1: \n",
    "                i += 1\n",
    "                continue\n",
    "            cnt = 1\n",
    "            while i < n and nums[i] == 0:\n",
    "                i += 1\n",
    "                cnt += 1\n",
    "            i += 1\n",
    "            ans = ans * cnt % MOD\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        ans = 1\n",
    "        last = -1\n",
    "        MOD = 10**9 + 7\n",
    "        for i,x in enumerate(nums):\n",
    "            if x:\n",
    "                if last != -1:\n",
    "                    ans *= i - last\n",
    "                    ans  %=  MOD\n",
    "                last = i\n",
    "        return ans if last != -1 else 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        MOD = 10 ** 9 + 7\n",
    "        ans, pre = 1, None\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 0: continue\n",
    "            else:\n",
    "                if pre != None:\n",
    "                    ans = (ans * (i - pre)) % MOD \n",
    "                pre = i\n",
    "        return ans if pre != None else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        last, res = 0, 1\n",
    "        while last < len(nums) and nums[last] == 0:\n",
    "            last += 1\n",
    "        if last == len(nums):\n",
    "            return 0\n",
    "        for i in range(last + 1, len(nums)):\n",
    "            if nums[i]:\n",
    "                res = res * (i - last) % (10 ** 9 + 7)\n",
    "                last = i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        if 1 not in nums:\n",
    "            return 0\n",
    "        res = 1\n",
    "        flag = False\n",
    "        count = 1\n",
    "        MOD= 10**9 + 7\n",
    "        for i in nums:\n",
    "            if i == 1 and not flag:\n",
    "                flag = True\n",
    "                continue\n",
    "            if i == 0 and flag:\n",
    "                count += 1\n",
    "                continue\n",
    "            if i == 1 and flag:\n",
    "                res *= count\n",
    "                count = 1\n",
    "                continue\n",
    "            # if i == 0 and not flag:\n",
    "            #     flag = False\n",
    "        return res % MOD\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        MOD = 10 ** 9 + 7\n",
    "        ans, pre = 1, -1\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 0: continue\n",
    "            if pre >= 0:\n",
    "                ans *= (i - pre) \n",
    "            pre = i\n",
    "        return 0 if pre < 0 else ans % MOD\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        cnt=Counter(nums)\n",
    "        c=cnt[1]\n",
    "        check=False\n",
    "        ans=1\n",
    "        mod=10**9+7\n",
    "        if c==0:\n",
    "            return 0\n",
    "        p=0\n",
    "        for x in nums:\n",
    "            if x==1:\n",
    "                if not check:\n",
    "                    check=True\n",
    "                else:\n",
    "                    ans*=p+1\n",
    "                    ans%=mod\n",
    "                p=0\n",
    "            else:\n",
    "                p+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        mod = 10 ** 9 + 7\n",
    "        res, pre = 1, -1\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 1:\n",
    "                if pre >= 0:\n",
    "                    res = res * (i - pre) % mod\n",
    "                pre = i\n",
    "        return 0 if pre == -1 else res\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        ans=1\n",
    "        pre=-1\n",
    "        mod=10**9+7\n",
    "\n",
    "        for i,v in enumerate(nums):\n",
    "            if v==1:\n",
    "                if pre>=0:\n",
    "                    ans=ans*(i-pre)%mod\n",
    "                    pre=i\n",
    "                else:\n",
    "                    pre=i\n",
    "        if sum(nums)==0:\n",
    "            return 0\n",
    "        return ans%mod\n",
    "        \n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        ans = 1\n",
    "        i = 0       \n",
    "        while i < len(nums) and nums[i]!= 1:           \n",
    "            i += 1\n",
    "        pre = i\n",
    "\n",
    "        if i == len(nums):\n",
    "            return 0\n",
    "        \n",
    "        while i < len(nums)-1:\n",
    "            i += 1\n",
    "            #print(ans,i,pre)\n",
    "            if nums[i] == 1:\n",
    "                ans *= i-pre\n",
    "                pre = i\n",
    "            \n",
    "               \n",
    "        return ans%(10**9+7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        MOD = 10 ** 9 + 7\n",
    "        ans, pre = 1, -1\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 0: continue\n",
    "            if pre >= 0:\n",
    "                ans = ans * (i - pre) % MOD\n",
    "            pre = i\n",
    "        return 0 if pre < 0 else ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        MOD = 10**9 + 7\n",
    "        ans = 1\n",
    "        prev = -1\n",
    "        for i, num in enumerate(nums):\n",
    "            if num == 1:\n",
    "                if prev != -1:\n",
    "                    ans *= (i - prev)\n",
    "                prev = i\n",
    "        if prev == -1:\n",
    "            return 0\n",
    "        return ans % MOD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        mod = int(1e9 + 7)\n",
    "\n",
    "\n",
    "        ans = 1\n",
    "\n",
    "        flag = False\n",
    "        pre = None\n",
    "\n",
    "        for i, n in enumerate(nums):\n",
    "            if n == 1:\n",
    "                if not flag:\n",
    "                    flag = True\n",
    "                    pre = i \n",
    "                else:\n",
    "                    ans *= i - pre\n",
    "                    ans %= mod\n",
    "                    pre = i\n",
    "\n",
    "        if pre is None:\n",
    "            return 0\n",
    "        else:\n",
    "            return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        '''\n",
    "        根据题意，需要在每两个 111 之间画一条分割线，有 xxx 个 000 就可以画 x+1x+1x+1 条分割线\n",
    "        '''\n",
    "        Mod = 10 ** 9 + 7\n",
    "        ans,pre =1,-1\n",
    "        for i,x in enumerate(nums):\n",
    "            if x == 0:\n",
    "                continue\n",
    "            if pre >= 0:\n",
    "                ans = ans * (i - pre) % Mod\n",
    "            pre = i\n",
    "\n",
    "        return 0 if pre < 0 else ans\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        # 1中间夹的0的数量+1相乘\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            if nums[i] == 1:\n",
    "                break\n",
    "        else:\n",
    "            return 0\n",
    "        cnt = 1\n",
    "        ans = 1\n",
    "        for j in range(i+1, n):\n",
    "            if nums[j] == 0:\n",
    "                cnt += 1\n",
    "            else:\n",
    "                ans *= cnt\n",
    "                cnt = 1\n",
    "        return ans % (10 ** 9 + 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        MOD = 10 ** 9 + 7\n",
    "        ans, pre = 1, -1\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 0: continue\n",
    "            if pre >= 0:\n",
    "                ans = ans * (i - pre) % MOD\n",
    "            pre = i\n",
    "        return 0 if pre < 0 else ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        ans = 1\n",
    "        Mod = int(1e9 +7)\n",
    "        pre, now = -1, 0\n",
    "        \n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 1:\n",
    "                if pre > 0:\n",
    "                    #print(i, pre )\n",
    "                    ans = (ans * (i - pre + 1)) % Mod \n",
    "                pre = i + 1\n",
    "                \n",
    "        if pre == -1:\n",
    "            return 0 \n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        MOD = 10 ** 9 + 7\n",
    "        ans, pre = 1, -1\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 0:  \n",
    "                continue\n",
    "            if pre >= 0:\n",
    "                ans = ans * (i - pre) % MOD\n",
    "            pre = i\n",
    "        return 0 if pre < 0 else ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        MOD = 10 ** 9 + 7\n",
    "        # 1中间夹的0的数量+1相乘\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 1:\n",
    "                break\n",
    "        else:\n",
    "            return 0\n",
    "        cnt = 1\n",
    "        ans = 1\n",
    "        for x in nums[i+1:]:\n",
    "            if x == 0:\n",
    "                cnt += 1\n",
    "            else:\n",
    "                ans = ans * cnt % MOD\n",
    "                cnt = 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        MOD = 10 ** 9 + 7\n",
    "        ans, pre = 1, -1\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 0: continue\n",
    "            if pre >= 0:\n",
    "                ans = ans * (i - pre) % MOD\n",
    "            pre = i\n",
    "        return 0 if pre < 0 else ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        start_cnt=False\n",
    "        cnt=0\n",
    "        ans=1\n",
    "        MOD=10**9+7\n",
    "        for i in nums:\n",
    "            if start_cnt and i==0:\n",
    "                cnt+=1\n",
    "            elif not start_cnt and i==1:\n",
    "                start_cnt=True\n",
    "            elif start_cnt and i==1:\n",
    "                ans*=cnt+1\n",
    "                ans%=MOD\n",
    "                cnt=0\n",
    "        return ans if start_cnt else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        ans = 1\n",
    "        MOD = 10**9+7\n",
    "        i = 0       \n",
    "        while i < len(nums) and nums[i]!= 1:           \n",
    "            i += 1\n",
    "        pre = i\n",
    "\n",
    "        if i == len(nums):\n",
    "            return 0\n",
    "        \n",
    "        while i < len(nums)-1:\n",
    "            i += 1\n",
    "            #print(ans,i,pre)\n",
    "            if nums[i] == 1:\n",
    "                ans = ans * (i-pre)%MOD\n",
    "                pre = i\n",
    "                         \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        g = []\n",
    "        a = 0\n",
    "        for n in nums:\n",
    "            if n == 0:\n",
    "                a += 1\n",
    "            else:\n",
    "                g.append(a+1)\n",
    "                a = 0\n",
    "        else:\n",
    "            if g: g.append(a+1)\n",
    "        return reduce(lambda a,b:a*b, g[1:-1], 1)%(10**9+7) if g else 0\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        if sum(nums) <= 1:\n",
    "            return sum(nums)\n",
    "        left, res = -1, []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == 1:\n",
    "                if left == -1:\n",
    "                    left = i\n",
    "                else:\n",
    "                    res.append(i-left)\n",
    "                    left = i\n",
    "        cnt = 1\n",
    "        for i in range(len(res)):\n",
    "            cnt *= res[i]\n",
    "        return cnt%(10**9+7)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        flag=0\n",
    "        res=1\n",
    "        count=0\n",
    "        for x in nums:\n",
    "            if x==1:                               \n",
    "                if flag==0:\n",
    "                    flag=1\n",
    "                    count=0\n",
    "                    continue\n",
    "                flag=1\n",
    "                res*=(count+1)\n",
    "                count=0\n",
    "            else:\n",
    "                count+=1\n",
    "        return (res%1000000007)*flag "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        res = 1\n",
    "        pos = None\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 1:\n",
    "                if pos == None:\n",
    "                    pos = i\n",
    "                    continue\n",
    "                if pos != None:\n",
    "                    res *= (i - pos)\n",
    "                    pos = i\n",
    "        if pos == None:\n",
    "            return 0\n",
    "        else:\n",
    "            return res % (10 ** 9 + 7)\n",
    "\n",
    "\n",
    "             "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        find1=False\n",
    "        c=0\n",
    "        ret=1\n",
    "        mod=pow(10,9)+7\n",
    "        for i,k in enumerate(nums):\n",
    "            if k==1:\n",
    "                if find1:\n",
    "                    ret=ret*c%mod\n",
    "                c=1\n",
    "                find1=True \n",
    "            else:\n",
    "                c+=1\n",
    "        if not find1:\n",
    "            return 0\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        MOD = 10 ** 9 + 7\n",
    "        ans, pre = 1, -1\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 0: continue\n",
    "            if pre >= 0:\n",
    "                ans = ans * (i - pre) % MOD\n",
    "            pre = i\n",
    "        return 0 if pre < 0 else ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        ans = 1\n",
    "        i = 0       \n",
    "        while i < len(nums) and nums[i]!= 1:           \n",
    "            i += 1\n",
    "        pre = i\n",
    "\n",
    "        if i == len(nums):\n",
    "            return 0\n",
    "        \n",
    "        while i < len(nums)-1:\n",
    "            i += 1\n",
    "            #print(ans,i,pre)\n",
    "            if nums[i] == 1:\n",
    "                ans *= i-pre\n",
    "                pre = i\n",
    "            \n",
    "               \n",
    "        return ans%(10**9+7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        MOD = 10**9+7\n",
    "        gap = []\n",
    "        jud = False\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == 1:\n",
    "                if jud:\n",
    "                    gap.append(i-pre)\n",
    "                    pre = i\n",
    "                else:\n",
    "                    jud = True\n",
    "                    pre = i\n",
    "        if len(set(nums)) == 1:\n",
    "            return 0 if nums[0] == 0 else 1\n",
    "        res =  1\n",
    "        for g in gap:\n",
    "            res *= g\n",
    "        return res%MOD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        if sum(nums) == 0: return 0\n",
    "        ans = 1\n",
    "        pre = -1\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == 1:\n",
    "                if pre != -1:\n",
    "                    ans = ans * (i - pre) % 1000000007\n",
    "                pre = i\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        MOD = 10 ** 9 + 7\n",
    "        n = len(nums)\n",
    "        arr = []\n",
    "        j = -1\n",
    "        for i in range(n):\n",
    "            if nums[i] == 1:\n",
    "                if j >= 0:\n",
    "                    arr.append(i - j)\n",
    "                j = i\n",
    "        if not arr:\n",
    "            return int(j >= 0)\n",
    "        return reduce(lambda x,y:x*y, arr) % MOD\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        MOD = 10 ** 9 + 7\n",
    "        ans, pre = 1, -1\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 0: continue\n",
    "            if pre >= 0:\n",
    "                ans = ans * (i - pre) % MOD\n",
    "            pre = i\n",
    "        return 0 if pre < 0 else ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        '''if nums.count(1) == 0:\n",
    "            return 0\n",
    "        res = 1\n",
    "        i = nums.index(1)\n",
    "        j = i+1\n",
    "        while j < len(nums):\n",
    "            if nums[j] == 1:\n",
    "                res *= j-i\n",
    "                i = j\n",
    "            j+=1\n",
    "        return res%(10**9+7)'''\n",
    "        res = 1\n",
    "        mod = int(1e9+7)\n",
    "        try:\n",
    "            index = nums.index(1)\n",
    "        except ValueError:\n",
    "            return 0\n",
    "        while True:\n",
    "            try:\n",
    "                nxt = nums.index(1,index+1)\n",
    "            except ValueError:\n",
    "                return res\n",
    "            res = (res*(nxt-index))%mod\n",
    "            index = nxt\n",
    "'''class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        ret, mod = 1, int(1e9 + 7)\n",
    "        try:\n",
    "            index = nums.index(1)\n",
    "        except ValueError:\n",
    "            return 0\n",
    "        \n",
    "        while True:\n",
    "            try:\n",
    "                nxt = nums.index(1, index + 1)\n",
    "            except ValueError:\n",
    "                return ret\n",
    "            ret = (ret * (nxt - index)) % mod\n",
    "            index = nxt\n",
    "'''\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "mod = 10**9 + 7\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        ans = 1\n",
    "        if 1 not in nums:\n",
    "            return 0\n",
    "        i = nums.index(1)\n",
    "        pre = i\n",
    "        for j in range(i+1, n):\n",
    "            if nums[j]:\n",
    "                mid = j - pre-1\n",
    "                ans *= (mid+1)\n",
    "                pre = j\n",
    "        return ans % mod"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        last_one = -1\n",
    "        res = 0\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] != 1:\n",
    "                continue\n",
    "            if last_one == -1:\n",
    "                last_one = i\n",
    "                res = 1\n",
    "            else:\n",
    "                res *= (i - last_one)\n",
    "                last_one = i\n",
    "\n",
    "\n",
    "        return res % (10 ** 9 + 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        MOD = 10 ** 9 + 7\n",
    "        ans, pre = 1, -1\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 0: continue\n",
    "            if pre >= 0:\n",
    "                ans = ans * (i - pre) % MOD\n",
    "            pre = i\n",
    "        return 0 if pre < 0 else ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        MOD = 10 ** 9 + 7\n",
    "        ans, pre = 1, -1\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 0: continue\n",
    "            if pre >= 0:\n",
    "                ans = ans * (i - pre) % MOD\n",
    "            pre = i\n",
    "        return 0 if pre < 0 else ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        MOD = 10 ** 9 + 7\n",
    "        ans = 1\n",
    "        pre = -1\n",
    "        for i,num in enumerate(nums):\n",
    "            if num == 0:\n",
    "                continue\n",
    "            if pre != -1:\n",
    "                ans = ans * (i - pre) % MOD\n",
    "            pre = i\n",
    "        return ans if pre != -1 else 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        cnt, pos, n = 0, 0, len(nums)\n",
    "        gap = list()\n",
    "        mod = 1e9 + 7\n",
    "\n",
    "        while pos < n and nums[pos] is 0:\n",
    "            pos += 1\n",
    "        \n",
    "        if pos >= n:\n",
    "            return 0\n",
    "        \n",
    "        while pos < n - 1:\n",
    "            pos += 1\n",
    "            cnt += 1\n",
    "            if nums[pos] == 1:\n",
    "                gap.append(cnt)\n",
    "                cnt = 0\n",
    "\n",
    "        ans = 1\n",
    "        for x in gap:\n",
    "            ans = ans * x % mod\n",
    "        return int(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\r\n",
    "        #求1和1之间的0的个数\r\n",
    "        n = len(nums)\r\n",
    "        pre = -1\r\n",
    "        ans = 1\r\n",
    "        MOD = 10**9 + 7\r\n",
    "        for i in range(n):\r\n",
    "            if nums[i] == 1:\r\n",
    "                if pre != -1:\r\n",
    "                    ans *= (i - pre) % MOD\r\n",
    "                pre = i\r\n",
    "        return ans % MOD if pre != -1 else 0\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        mod = 10 ** 9 + 7\n",
    "        if sum(nums) == 0:\n",
    "            return 0\n",
    "        elif sum(nums) == 1:\n",
    "            return 1\n",
    "        res = []\n",
    "        i = 0\n",
    "        for per in nums:\n",
    "            if per == 0:\n",
    "                i += 1\n",
    "            else:\n",
    "                res.append(i)\n",
    "                i = 0\n",
    "        n = len(res)\n",
    "        ans = 1\n",
    "        j = 0\n",
    "        if nums[0] == 0:\n",
    "            j = 1\n",
    "        while j < n:\n",
    "            ans *= (res[j] + 1)\n",
    "            ans %= mod\n",
    "            j += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        if nums.count(1) == 0:\n",
    "            return 0\n",
    "        t = \"\"\n",
    "        for i in nums:\n",
    "            t+=str(i)\n",
    "        r = t.split('1')\n",
    "        x = nums[0]\n",
    "        y = nums[len(nums)-1]\n",
    "        if x != 1:\n",
    "            r.pop(0)\n",
    "        if y != 1:\n",
    "            r.pop()\n",
    "        res = 1\n",
    "        for i in r:\n",
    "            n = len(i)\n",
    "            res = (n+1)*res\n",
    "        return res%(10**9+7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        if 1 not in nums:\n",
    "            return 0 \n",
    "\n",
    "        pos, ans = [ i for i in range(len(nums)) if nums[i]], 1\n",
    "        for i in range(1, len(pos)):\n",
    "            ans = ans * (pos[i] - pos[i - 1]) % (10 ** 9 + 7)\n",
    "\n",
    "        return ans  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        lst = list()\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == 1:\n",
    "                lst.append(i)\n",
    "        if not lst:\n",
    "            return 0\n",
    "        if len(lst) == 1:\n",
    "            return 1\n",
    "        ans = 1\n",
    "        for i in range(len(lst)-1,0,-1):\n",
    "            ans *= lst[i]-lst[i-1] \n",
    "        return ans%(10**9+7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        zeros = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == 1:\n",
    "                zeros.append(i)\n",
    "        ans = 1\n",
    "        if len(zeros) == 0:\n",
    "            return 0\n",
    "        for i in range(len(zeros)-1):\n",
    "            ans = ans * (zeros[i+1]-zeros[i]) % 1000000007\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        A=list()\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]==1:\n",
    "                A.append(i)\n",
    "        c=1\n",
    "        if len(A)==0:\n",
    "            return 0\n",
    "        if len(A)>1:\n",
    "            for i in range(1,len(A)):\n",
    "                c=c*(A[i]-A[i-1])%(10**9+7)\n",
    "        return c\n",
    "                \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        l = []\n",
    "        for idx, num in enumerate(nums):\n",
    "            if num == 1:\n",
    "                l.append(idx)\n",
    "\n",
    "        if len(l) == 0:\n",
    "            return 0\n",
    "        \n",
    "        if len(l) == 1:\n",
    "            return 1\n",
    "        \n",
    "        MOD = 10**9+7\n",
    "        ans = 1\n",
    "        for i in range(1, len(l)):\n",
    "            ans *= (l[i]-l[i-1])\n",
    "            ans %= MOD\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        idx_1 = []\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 1:\n",
    "                idx_1.append(i)\n",
    "        \n",
    "        mod = 1e9 + 7\n",
    "        res = min(1, len(idx_1))\n",
    "        for i in range(1, len(idx_1), 1):\n",
    "            res = res * (idx_1[i]-idx_1[i-1]) % mod\n",
    "        return int(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        mod = 10 ** 9 + 7\n",
    "        pos = []\n",
    "        for i, v in enumerate(nums):\n",
    "            if v:\n",
    "                pos.append(i)\n",
    "        if len(pos) == 0: return 0\n",
    "        ans = 1\n",
    "        for i in range(1, len(pos)):\n",
    "            cnt = pos[i] - pos[i-1]\n",
    "            ans = ans * cnt % mod\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        mod = 10**9+7\n",
    "        tot = 0\n",
    "        for v in nums:\n",
    "            tot += v\n",
    "        if tot == 0:\n",
    "            return 0\n",
    "        elif tot == 1:\n",
    "            return 1\n",
    "        ans = 1\n",
    "        d = []\n",
    "        for i in range(n):\n",
    "            if nums[i]:\n",
    "                d.append(i)\n",
    "        for i in range(1,len(d),1):\n",
    "            ans = ans*(d[i]-d[i-1])%mod\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        # 0 1 0 0 1 1 0 0 1 0 1 0\n",
    "        # 0 0 1 0 0 0\n",
    "        # 1 0 1 0 0 0\n",
    "        # 1 0 0 0 0 1\n",
    "        # 0 1 0 1 0 0\n",
    "        mod = 10 ** 9 + 7\n",
    "        i, j = 0, 0\n",
    "        n = len(nums)\n",
    "        res = []\n",
    "        for i, x in enumerate(nums):\n",
    "            if x:\n",
    "                res.append(i)\n",
    "        if len(res) == 0:\n",
    "            return 0\n",
    "        if len(res) == 1:\n",
    "            return 1\n",
    "        ans = 1\n",
    "        for i in range(1, len(res)):\n",
    "            ans = (ans * (res[i] - res[i - 1])) % mod\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        g = []\n",
    "        for i, x in enumerate(nums):\n",
    "            if x == 1:\n",
    "                g.append(i)\n",
    "        ans = len(g) != 0\n",
    "        for i in range(1, len(g)):\n",
    "            ans *= g[i] - g[i-1]\n",
    "        mod = 10 ** 9 + 7\n",
    "        return ans % mod"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        mod = int(1e9 + 7)\n",
    "        n = len(nums)\n",
    "        dp = [0] * n \n",
    "\n",
    "        if nums[0] == 1:\n",
    "            dp[0] = 1\n",
    "\n",
    "        \n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] == 0:\n",
    "                dp[i] = dp[i - 1]\n",
    "            else:\n",
    "                j = i - 1\n",
    "\n",
    "                while j >= 0 and nums[j] == 0:\n",
    "                    dp[i] += dp[j]\n",
    "                    dp[i] %= mod\n",
    "                    j -= 1\n",
    "\n",
    "                if j >= 0:\n",
    "                    dp[i] += dp[j]      \n",
    "                    dp[i] %= mod          \n",
    "                elif j == -1:\n",
    "                    dp[i] = 1\n",
    "\n",
    "\n",
    "        return dp[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        mx=10**9+7\n",
    "        n=len(nums)\n",
    "        l1=[]\n",
    "        for i in range(n):\n",
    "            if nums[i]==1:\n",
    "                l1.append(i)\n",
    "        if len(l1)==0: return 0\n",
    "        c1=[]\n",
    "        for i in range(len(l1)-1):\n",
    "            c1.append(l1[i+1]-l1[i])\n",
    "        # if len(c1)==0: return 1\n",
    "        ans=1\n",
    "        for c in c1:\n",
    "            ans=(ans*c)%mx\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        MOD = 10**9+7\n",
    "        idx = []\n",
    "        for i,v in enumerate(nums):\n",
    "            if v:\n",
    "                idx.append(i)\n",
    "        #print(idx)\n",
    "        res = 1\n",
    "        n = len(idx)\n",
    "        a = []\n",
    "        if not idx:\n",
    "            return 0\n",
    "        if n == 1:\n",
    "            return 1\n",
    "        for i in range(n-1):\n",
    "            a.append(idx[i+1]-idx[i])\n",
    "        for j in a:\n",
    "            res *= j\n",
    "        return res%MOD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:\n",
    "        # 从所有相邻的1中切开，然后相邻的1有多大的距离，那么结果偶需要乘以多少\n",
    "        idx = [i for i, x in enumerate(nums) if x == 1]                      \n",
    "        if not idx:return 0\n",
    "        Mod = 10 ** 9 + 7                                                    \n",
    "        res = 1                                                              \n",
    "        for x, y in zip(idx, idx[1:]):                                       \n",
    "            res = res * (y - x) % Mod                                        \n",
    "        return res                                                           "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
